//
//  MQLoginViewController.m
//  meiquan
//
//  Created by hejingjin on 2017/10/23.
//  Copyright © 2017年 mq. All rights reserved.
//

#import "MQLoginViewController.h"
#import "MQLoginViewss.h"
#import "MQBaseUserInfoModel.h"
#import "MQBaseTabBarViewController.h"
#import "NSString+Extension.h"
#import "MBProgressHUD+MJ.h"


@interface MQLoginViewController ()

@property(nonatomic,strong)MQLoginViewss *loginviewss;

@end

@implementation MQLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"登录";
    
    [self setUpMainInterface];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUpMainInterface{
    
    self.view.backgroundColor = BSTColorFromRGB(0xefefef);
    
    
    _loginviewss = [MQLoginViewss loadLoginviewss];
    
    _loginviewss.bst_x = 0;
    _loginviewss.bst_y = 64;
    _loginviewss.bst_width = BSTScreenW;
    _loginviewss.bst_height = BSTScreenH - 64;
    
    _loginviewss.loginButton.layer.cornerRadius = 4.f;
    _loginviewss.loginButton.layer.masksToBounds = YES;
    [_loginviewss.loginButton addTarget:self action:@selector(loginButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_loginviewss];
    
}

-(void)loginButtonPressed:(UIButton *)sender{
//    [iToastUtil displayMsg:@"旧密码不能为空"];
    
    if ([NSString isBlankString:_loginviewss.phoneNumberTextFeild.text]) {
        [iToastUtil displayMsgByLongtime:@"请输入用户名"];
        return;
    }
    if ([NSString isBlankString:_loginviewss.passwordTextfield.text]) {
        [iToastUtil displayMsgByLongtime:@"请输入密码"];
        return;
    }

    NSString *phonestr = _loginviewss.phoneNumberTextFeild.text;
    NSString *password = _loginviewss.passwordTextfield.text;
    
    [[MQBaseUserInfoModel shareInstant] loadUserinfoFromSadeBox];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:phonestr forKey:@"telephone"];
    [param setValue:password forKey:@"password"];
    [param setValue:@"ios" forKey:@"loginType"];
    [param setValue:[MQBaseUserInfoModel shareInstant].deviceToken forKey:@"deviceToken"];

    [MBProgressHUD showMessage:@"正在登陆,请稍后..."];

    
    [MQRequestTools POSTWithMethod:login params:param success:^(id json) {
       
        [MBProgressHUD hideHUD];

        NSDictionary *dic = [json valueForKey:@"data"];
        MQBaseUserInfoModel *userinfomodel = [MQBaseUserInfoModel shareInstant];
        [userinfomodel loadUserinfoFromSadeBox];
        userinfomodel.userNormalId = [dic valueForKey:@"userNormalId"];
        userinfomodel.userId =[NSString stringWithFormat:@"%ld",[[dic valueForKey:@"userId"] integerValue]];
        userinfomodel.token = [dic valueForKey:@"token"];
        userinfomodel.isAgent = [[dic valueForKey:@"isAgent"] boolValue];
        userinfomodel.username = [dic valueForKey:@"username"];
        userinfomodel.weixin = [dic valueForKey:@"weixin"];
        userinfomodel.agentIds = [dic valueForKey:@"agentIds"];
        [userinfomodel saveUserInfoToSadeBox];
        AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        MQBaseTabBarViewController *tabbarvc = [[MQBaseTabBarViewController alloc] init];
        appDelegate.window.rootViewController = tabbarvc;
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];

         [iToastUtil displayMsgBytime:@"登录失败请稍后再试" duration:600];
    }];
    
   
}

@end
